package com.tz.fruit.dao.OrderDao.Impl;

import com.tz.fruit.dao.OrderDao.OrderDao;
import com.tz.fruit.entity.Order;
import com.tz.fruit.entity.OrderItem;
import com.tz.fruit.entity.OrderStatus;
import com.tz.fruit.util.DateUtil;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/7/19.
 */
@Repository
@Transactional
public class OrderDaoImpl implements OrderDao {
    @Autowired
    private SessionFactory sessionFactory;

    public Session getSession(){
        return  sessionFactory.getCurrentSession();
    }

    @Override
    public List<Order> findAll() {

       List<Order>orders=getSession().createQuery("from Order ").list();
        for(Order  o:orders){
           o.setCreateDate(DateUtil.parse("yyyy/MM/dd hh:mm:ss",o.getCreateDate().toString()));
           o.setPayDate(DateUtil.parse("yyyy/MM/dd hh:mm:ss",o.getPayDate().toString()));
        }
        return  orders;
    }

    @Override
    public void save(OrderItem orderItem) {
      getSession().save(orderItem);
    }

    @Override
    public void deleteById(Long id) {
        Order order= (Order) getSession().get(Order.class,id);
        getSession().delete(order);
    }

    @Override
    public Long findBy(Long uid, String orderNo, Date start, Date end, OrderStatus status) {
        Criteria criteria=getSession().createCriteria(Order.class,"o");

        if(uid!=null){
            criteria.add(Restrictions.eq("o.user.userId", uid));

        }
        if(orderNo!=null && orderNo.trim().length()>0){
            criteria.add(Restrictions.like("o.orderNo", "%"+orderNo+"%"));
        }
        if(start!=null && end!=null  && start.before(end)){
            criteria.add(Restrictions.between("o.createDate", start, end));
        }
        if(status!=null){
            criteria.add(Restrictions.eq("o.orderStatus", status));

        }
        return (Long) criteria.setProjection(Projections.rowCount()).uniqueResult();
    }

    @Override
    public List<Order> findByConditions(Long uid, String orderNo, Date start, Date end, String pageNow, String pageSize, OrderStatus status) {
        Criteria  criteria=getSession().createCriteria(Order.class,"o");
        if(uid!=null){
            criteria.add(Restrictions.eq("o.user.userId",uid));

        }
        if(orderNo!=null && orderNo.trim().length()>0){
            criteria.add(Restrictions.like("o.orderNo", "%"+orderNo+"%"));
        }
        if(start!=null && end!=null  && start.before(end)){
            criteria.add(Restrictions.between("o.createDate", start, end));
        }
        if(status!=null){
            criteria.add(Restrictions.eq("o.orderStatus", status));

        }
        if(pageNow!=null && pageNow.trim().length()>0){

            criteria.setFirstResult((Integer.valueOf(pageNow)-1)*(Integer.valueOf(pageSize)));
        }
        System.out.println("======================"+pageSize);
        if(pageSize!=null && pageSize.trim().length()>0){

            criteria.setMaxResults(Integer.valueOf(pageSize));
        }
        return  criteria.list();
    }


}
